package com.bigflexlib.x.Math.range
{


	/**
	 * 
	 * 
	 * 
	 * @author aldo.bucchi@gmail.com
	 * @see http://en.wikipedia.org/wiki/Interval_(mathematics)#The_ISO_notation
	 * 
	 */
	public class Range
	{
		
		//////////////////////////////////////
		// minInclusive
		//////////////////////////////////////
		
		private var _minInclusive:Boolean;
		
		public function get minInclusive( ):Boolean
		{
			return _minInclusive;
		}
		
		
		//////////////////////////////////////
		// maxInclusive
		//////////////////////////////////////
		
		private var _maxInclusive:Boolean;
		
		public function get maxInclusive( ):Boolean
		{
			return _maxInclusive;
		}
		
		
		
		
		
		
		
		//////////////////////////////////////
		// min
		//////////////////////////////////////

		private var _min:Number;
		
		public function get min( ):Number
		{
			return _min;
		}
		
		
		
		//////////////////////////////////////
		// max
		//////////////////////////////////////
		
		private var _max:Number;
		
		public function get max( ):Number
		{
			return _max;
		}
		
		


		public function Range( min:Number, max:Number, minInclusive:Boolean=true, maxInclusive:Boolean=true )
		{

			if ( min > max )
			{
				_max = min;
				_maxInclusive = minInclusive;
				_min = max;
				_minInclusive = maxInclusive;
			}
			else
			{
				_min = min;
				_minInclusive = minInclusive;
				_max = max;
				_maxInclusive = maxInclusive;
			}

		}
		
		


		
		public function containsValue( value:Number ):Boolean
		{
			
			if ( value > _min && value < _max )
				return true;
				
			if ( _minInclusive && value == _min )
				return true;
				
			if ( _maxInclusive && value == _max )
				return true;
			
			return false;

		}
		
		
		
		public function toString( ):String
		{
			var arr:Array = new Array( );
			arr.push( _minInclusive ? "[" : "]" );
			arr.push( _min == Number.NEGATIVE_INFINITY ? "-" : _min );
			arr.push( "," );
			arr.push( _max == Number.POSITIVE_INFINITY ? "+" : _max );			
			arr.push( _maxInclusive ? "]" : "[" );			
			return arr.join( " " );
		}
		
		
		
		
		
		
		/**
		 * 
		 * Positive and negative infinite are denoted by + and - symbols.
		 * Numbers can be floating point, using dot decimal notation.
		 * 
		 * @return 
		 * @see http://en.wikipedia.org/wiki/Interval_(mathematics)#The_ISO_notation
		 * 
		 */		
		public static function fromISO31Interval( str:String ):Range
		{
			// TODO: optimize algorithm. Maybe some pooling too
			// remove spaces
			str = str.split(" ").join("");
			var leftBracket		:String = str.substr( 0, 1 );											
			var rightBracket	:String = str.substr( str.length-1, 1 );
			
			str = str.substr( 1, str.length-2 );
			var numbers:Array = str.split( "," );
		
			return new Range( 
							numbers[0] == "-"  ? Number.NEGATIVE_INFINITY : Number( numbers[0] ), 
							numbers[1] == "+"  ? Number.POSITIVE_INFINITY : Number( numbers[1] ),
							(leftBracket == "["),
							(rightBracket == "]")
						);
		}
		

	}
}




